//
//  MoreQuanziViewController.m
//  YiqiMobile
//
//  Created by henry on 13-4-10.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "MoreQuanziViewController.h"
#import "Utility.h"
#import "Config.h"
#import "MoreQuanziViewCell.h"
#import "AppDelegate.h"
#import "Utility.h"
#import "MBProgressHUD.h"
#import "JSON.h"
#import "DefineAction.h"
#import "Config.h"

@interface MoreQuanziViewController ()
-(IBAction)onclickButtonAction:(UIButton*)sender;
@end

@implementation MoreQuanziViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.titleView=[Utility getTitleView:@"更多圈子"];
    
    /*UIButton *visitButton=[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"moreqz_visit_normal.png" lightedImage:@"moreqz_visit_hover.png" frame:CGRectMake(0, 0, 107, 38) title:nil tag:QUANZIMOREBUTTONTAG];
    visitButton.selected=YES;
    [self.view addSubview:visitButton];
    
    [self.view addSubview:[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"moreqz_focus_normal.png" lightedImage:@"moreqz_focus_hover.png" frame:CGRectMake(107, 0, 106, 38) title:nil tag:QUANZIMOREBUTTONTAG+1]];
    
    [self.view addSubview:[Utility customButton:self action:@selector(onclickButtonAction:) normalImage:@"moreqz_chengyuan_normal.png" lightedImage:@"moreqz_chengyuan_hover.png" frame:CGRectMake(213, 0, 107, 38) title:nil tag:QUANZIMOREBUTTONTAG+2]];*/
	// Do any additional setup after loading the view.
    
    float tableHeight=self.view.frame.size.height-self.navigationController.navigationBar.frame.size.height;
    CGRect tableViewFrame=CGRectMake(0, 0, UISCREEN_WIDTH, tableHeight);
    self.mytableView=[[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
    self.mytableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    //self.mytableView.separatorColor=[UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1.0];
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    
    _refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.mytableView.bounds.size.height, self.view.frame.size.width, self.mytableView.bounds.size.height)];
    _refreshHeaderView.delegate = self;
    [self.mytableView addSubview:_refreshHeaderView];
    [_refreshHeaderView refreshLastUpdatedDate];
}

-(IBAction)onclickButtonAction:(UIButton*)sender{
    for (int i=0; i<3; i++) {
        UIButton *button=(UIButton*)[self.view viewWithTag:QUANZIMOREBUTTONTAG+i];
        button.selected=NO;
    }
    sender.selected=YES;
    switch (sender.tag-QUANZIMOREBUTTONTAG) {
        case 0:{//最近访问的
            
            break;
        }
        case 1:{//推荐关注
            
            break;
        }
        case 2:{//成员招募中
            
            break;
        }
        
    }
    
}

/*
 id	N	查询的用户长ID
 jr.curPageNum	N	当前页。首页1开始
 u.u0	N	当前登录者ID
 */
-(void) loadDataFromServer:(int)currpage{
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    if (app.status==NotReachable) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
        return;
    }
      NSString *userId=[[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    NSString *url=[NSString stringWithFormat:@"%@/%@",[Utility  getServerUrl],CIRCLELIST];
    NSMutableDictionary *param=[NSMutableDictionary dictionaryWithDictionary:@{@"id":userId,@"jr.curPageNum":[NSString stringWithFormat:@"%d",currpage],@"u.u0":userId}];
   
    self.request=[[URLRequest alloc] initWithPostURL:url andDelegate:self andPostData:param andFile:nil];
    [[MBProgressHUD showHUDAddedTo:self.view animated:YES] setLabelText:LOADING_TITLE_DEFAULT];
    [self.request startLoadData];
}


- (void)requestFinished:(id)context{
    [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
    @try{
        NSDictionary *result=[context JSONValue];
        if (result!=nil&&[[result  objectForKey:@"code"] integerValue]==0) {
            if ([[result  objectForKey:@"method"] isEqualToString:CIRCLELIST]) {
                NSDictionary *data=[result objectForKey:@"data"];
                [self.arrayList removeAllObjects];
                [self.arrayList addObjectsFromArray:[data objectForKey:@"nrList"]];
                [self.mytableView reloadData];
                [self doneLoadingTableViewData];
            }
            
        }else{
            [Utility customAlertWithMsg:PROCESSERROE andTitle:ALRRTTITLE andDelagate:nil];
        }
    }@catch (NSException *e) {
        [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
    }
}

- (void)requestFailed:(id)context error:(NSString *)error{
    [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:NO];
    [Utility customAlertWithMsg:NETWORTERROE andTitle:ALRRTTITLE andDelagate:nil];
}


//表视图委托
#pragma mark -
#pragma mark table view data source methods

//返回某个表视图有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayList count];
}

//设置行高度
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


//表视图显示表视图项时调用：第一次显示（根据视图大小显示多少个视图项就调用多少次）以及拖动时调用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"moreQuanziViewCell";
     NSDictionary *dict=[self.arrayList objectAtIndex:indexPath.row];
    MoreQuanziViewCell *cell =(MoreQuanziViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[MoreQuanziViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier: CellIdentifier];
    }
   
    cell.content.text=[dict objectForKey:@"str2"];
    cell.detailTitle.text=[NSString stringWithFormat:@"%@",[Utility datefromSeconds:[dict objectForKey:@"dat1"] andDateFormat:@"yyyy-MM-dd MM:hh:ss"]];
    return cell;
}



//某行已经被选中时调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // [tableView deselectRowAtIndexPath:indexPath animated:YES];//在弹出警告后自动取消选中表视图单元
    
    [Utility customAlertWithMsg:@"跳转到哪里呢" andTitle:ALRRTTITLE andDelagate:nil];
}

-(void)reloadTableViewDataSource{
    _reloading = YES;
    [self loadDataFromServer:1];
    [self.mytableView reloadData];
    
    
    
}

- (void)doneLoadingTableViewData{
    //  model should call this when its done loading
    _reloading = NO;
    [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.mytableView];
    
    
}
#pragma mark –
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    
}

#pragma mark –
#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
    
    [self reloadTableViewDataSource];
    
    
    
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
    
    return _reloading; // should return if data source model is reloading
    
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
    
    return [NSDate date]; // should return date data source was last changed
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
